using System.IO;
using System.Windows.Input;
using Bookvar.ObjectModel;
using Bookvar.ObjectModel.Actions;
using Bookvar.UI.Commands;

namespace Bookvar.UI.ViewModel
{
    public partial class AttachmentsWindowViewModel
    {
        private class AddAttachmentCommand : CommandModel
        {
            private readonly AttachmentsWindowViewModel viewModel;

            public AddAttachmentCommand( AttachmentsWindowViewModel viewModel )
                : base( BookvarCommands.AddAttachment )
            {
                this.viewModel = viewModel;
            }

            protected internal override void OnExecute( object sender, ExecutedRoutedEventArgs e )
            {
                var fileName = e.Parameter as string;

                if ( File.Exists( fileName ) )
                {
                    var attachment = new Attachment( Path.GetFileName( fileName ) );

                    IAction action = TopicActions.CreateAddAttachmentAction(
                        attachment, fileName, viewModel.Topic, viewModel.Mindmap );

                    ActionService.Execute( action );
                }
            }

            protected internal override void OnCanExecute( object sender, CanExecuteRoutedEventArgs e )
            {
                e.CanExecute = true;
                e.Handled = true;
            }
        }
    }
}